home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / tde40.zip / tdeasm.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  8KB  |  254 lines

  1. /*
  2.  * The DTE 5.1 editor used a big text buffer for editing a file.  The big
  3.  * text buffer holds an image of a file pretty much as it appears on
  4.  * disk.  In that big buffer, '\0' was used to delimit the files.  In TDE
  5.  * versions 2.10 and less, a big text buffer was also used, but ^Z was used
  6.  * instead of '\0' to delimit files.  A double linked list is used in TDE 2.2
  7.  * to hold text.
  8.  *
  9.  * With these functions written in assembly, this editor is fairly fast.
  10.  * I feel the need for speed.
  11.  *
  12.  * New editor name:  TDE, the Thomson-Davis Editor.
  13.  * Author:           Frank Davis
  14.  * Date:             June 5, 1991, version 1.0
  15.  * Date:             July 29, 1991, version 1.1
  16.  * Date:             October 5, 1991, version 1.2
  17.  * Date:             January 20, 1992, version 1.3
  18.  * Date:             February 17, 1992, version 1.4
  19.  * Date:             April 1, 1992, version 1.5
  20.  * Date:             June 5, 1992, version 2.0
  21.  * Date:             October 31, 1992, version 2.1
  22.  * Date:             April 1, 1993, version 2.2
  23.  * Date:             June 5, 1993, version 3.0
  24.  * Date:             August 29, 1993, version 3.1
  25.  * Date:             November 13, 1993, version 3.2
  26.  * Date:             June 5, 1994, version 4.0
  27.  *
  28.  * This modification of Douglas Thomson's code is released into the
  29.  * public domain, Frank Davis.  You may distribute it freely.
  30.  */
  31.  
  32. #include "tdestr.h"
  33. #include "common.h"
  34. #include "tdefunc.h"
  35.  
  36.  
  37. /*
  38.  * Name:    ptoul - pointer to unsigned long
  39.  * Purpose: convert a FAR pointer to unsigned long integer
  40.  * Date:    June 5, 1991
  41.  * Passed:  s:  a FAR pointer
  42.  * Notes:   combine the offset and segment like so:
  43.  *                offset       0000
  44.  *                segment   + 0000
  45.  *                          =======
  46.  *                            00000
  47.  *          result is returned in dx:ax
  48.  */
  49.  
  50. #if defined( __UNIX__ )
  51.  void *ptoul( void *s )
  52.  {
  53.     return( s );
  54.  }
  55. #else
  56.  unsigned long ptoul( void FAR *s )
  57.  {
  58.    ASSEMBLE {
  59.         mov     ax, WORD PTR s          /* ax = OFFSET of s */
  60.         mov     dx, WORD PTR s+2        /* dx = SEGMENT of s */
  61.         mov     bx, dx          /* put copy of segment in bx */
  62.         mov     cl, 12          /* cl = decimal 12 - shift hi word 3 hex digits */
  63.         shr     dx, cl          /* convert to 'real segment' */
  64.         mov     cl, 4           /* cl = 4  - shift hi word 1 hex digit left */
  65.         shl     bx, cl          /* shift bx - to add 3 digits of seg to 4 of off */
  66.         add     ax, bx          /* add low part of segment to offset */
  67.         adc     dx, 0           /* if carry, bump to next 'real' segment */
  68.    }
  69.  }
  70. #endif
  71.  
  72.  
  73. /*
  74.  * Name:    tabout
  75.  * Purpose: Expand tabs in display line
  76.  * Date:    October 31, 1992
  77.  * Passed:  s:  string pointer
  78.  *          len:  pointer to current length of string
  79.  * Notes:   Expand tabs in the display line according to current tab.
  80.  *          If eol display is on, let's display tabs, too.
  81.  */
  82. text_ptr tabout( text_ptr s, int *len )
  83. {
  84. text_ptr to;
  85. int space;
  86. int col;
  87. int i;
  88. int tab_size;
  89. int show_tab;
  90. int tab_len;
  91.  
  92. #if defined( __UNIX__ )
  93.  
  94.    /*
  95.     * for right now, no assembly in unix
  96.     */
  97.    tab_size = mode.ptab_size;
  98.    show_tab = mode.show_eol;
  99.    to  = (text_ptr)g_status.tabout_buff;
  100.    i = tab_len  = *len;
  101.    tab_size = mode.ptab_size;
  102.    show_tab = mode.show_eol;
  103.    to  = g_status.tabout_buff;
  104.    i = tab_len  = *len;
  105.    for (col=0; col < (MAX_LINE_LENGTH - (tab_size+1)) &&  i > 0; s++, i--) {
  106.       if (*s != '\t') {
  107.          *to++ = *s;
  108.          ++col;
  109.       } else {
  110.          space = tab_size - (col % tab_size);
  111.          col += space;
  112.          space--;
  113.          if (space > 0)
  114.             tab_len += space;
  115.          if (show_tab)
  116.             *to++ = '\t';
  117.          else
  118.             *to++ = ' ';
  119.          for (; space > 0; space--)
  120.             *to++ = ' ';
  121.       }
  122.    }
  123.    if (tab_len > MAX_LINE_LENGTH)
  124.       tab_len = MAX_LINE_LENGTH;
  125.    *len = g_status.tabout_buff_len = tab_len;
  126.    return( (text_ptr)g_status.tabout_buff );
  127.  
  128. #else
  129.  
  130.    tab_size = mode.ptab_size;
  131.    show_tab = mode.show_eol;
  132.    to  = (text_ptr)g_status.tabout_buff;
  133.    i = tab_len  = *len;
  134.  
  135.    ASSEMBLE {
  136.         push    si
  137.         push    di
  138.         push    ds
  139.         push    es
  140.  
  141.         mov     bx, WORD PTR tab_size   /* keep tab_size in bx */
  142.         xor     cx, cx                  /* keep col in cx */
  143.  
  144.         mov     di, WORD PTR to
  145.         mov     ax, WORD PTR to+2
  146.         mov     es, ax                  /* es:di == to or the destination */
  147.         mov     si, WORD PTR s
  148.         mov     ax, WORD PTR s+2
  149.         mov     ds, ax                  /* ds:si == s or the source */
  150.    }
  151. top:
  152.  
  153.    ASSEMBLE {
  154.         cmp     cx, MAX_LINE_LENGTH     /* are at end of tabout buffer? */
  155.         jge     get_out
  156.  
  157.         cmp     WORD PTR i, 0           /* are at end of string? */
  158.         jle     get_out
  159.  
  160.         lodsb                           /* al == BYTE PTR ds:si */
  161.         cmp     al, 0x09                /* is this a tab character? */
  162.         je      expand_tab
  163.  
  164.         stosb                           /* store character in es:di inc di */
  165.         inc     cx                      /* increment col counter */
  166.         dec     WORD PTR i              /* decrement string counter */
  167.         jmp     SHORT top
  168.    }
  169. expand_tab:
  170.  
  171.    ASSEMBLE {
  172.         mov     ax, cx
  173.         xor     dx, dx                  /* set up dx:ax for IDIV */
  174.         IDIV    bx                      /* col % tab_size */
  175.         mov     ax, bx                  /* put tab_size in bx */
  176.         sub     ax, dx                  /* ax = tab_size - (col % tab_size) */
  177.         mov     dx, ax                  /* put ax in dx */
  178.         add     cx, ax                  /* col += space */
  179.         cmp     cx, MAX_LINE_LENGTH     /* is col > MAX_LINE_LENGTH? */
  180.         jge     get_out                 /* yes, get out */
  181.         mov     ax, ' '                 /* save blank character in ax */
  182.         cmp     WORD PTR show_tab, 0    /* was show_tab flag set? */
  183.         je      do_the_tab
  184.         mov     ax, 0x09                /* put tab character in ax */
  185.    }
  186. do_the_tab:
  187.  
  188.    ASSEMBLE {
  189.         stosb                           /* store in es:di and incr di */
  190.         dec     dx
  191.         cmp     dx, 0                   /* any spaces left to fill? */
  192.         jle     end_of_space            /* no, get another character */
  193.         add     WORD PTR tab_len, dx    /* add spaces to string length */
  194.         mov     ax, ' '                 /* save blank character in ax */
  195.    }
  196. space_fill:
  197.  
  198.    ASSEMBLE {
  199.         cmp     dx, 0                   /* any spaces left to fill? */
  200.         jle     end_of_space            /* no, get another character */
  201.         stosb                           /* store ' ' in es:di and incr di */
  202.         dec     dx                      /* space-- */
  203.         jmp     SHORT space_fill        /* fill space */
  204.   }
  205. end_of_space:
  206.  
  207.    ASSEMBLE {
  208.         dec     WORD PTR i
  209.         jmp     SHORT top
  210.    }
  211. get_out:
  212.  
  213.    ASSEMBLE {
  214.         pop     es
  215.         pop     ds
  216.         pop     di
  217.         pop     si
  218.    }
  219.    if (tab_len > MAX_LINE_LENGTH)
  220.       tab_len = MAX_LINE_LENGTH;
  221.    *len = g_status.tabout_buff_len = tab_len;
  222.    return( (text_ptr)g_status.tabout_buff );
  223.  
  224. /*
  225.    tab_size = mode.ptab_size;
  226.    show_tab = mode.show_eol;
  227.    to  = g_status.tabout_buff;
  228.    i = tab_len  = *len;
  229.    for (col=0; col < (MAX_LINE_LENGTH - (tab_size+1)) &&  i > 0; s++, i--) {
  230.       if (*s != '\t') {
  231.          *to++ = *s;
  232.          ++col;
  233.       } else {
  234.          space = tab_size - (col % tab_size);
  235.          col += space;
  236.          space--;
  237.          if (space > 0)
  238.             tab_len += space;
  239.          if (show_tab)
  240.             *to++ = TAB_CHAR;
  241.          else
  242.             *to++ = ' ';
  243.          for (; space > 0; space--)
  244.             *to++ = ' ';
  245.       }
  246.    }
  247.    if (tab_len > MAX_LINE_LENGTH)
  248.       tab_len = MAX_LINE_LENGTH;
  249.    *len = g_status.tabout_buff_len = tab_len;
  250.    return( (text_ptr)g_status.tabout_buff );
  251. */
  252. #endif
  253. }
  254.